import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';
import {ConfigEnum} from '/@/enums/httpEnum'

enum Api {
  list = '/ost-service/table/listTable',
  save = '/ost-service/table/saveTable',
  get = '/ost-service/table/getTable',
  edit ='/ost-service/table/updateTable',
  deleteTable = '/ost-service/table/deleteTable',
  deleteBatch = '/ost-service/table/batchDeleteTable',

  listCategory = '/ost-service/table/listCategory',
  saveCategory = '/ost-service/table/saveCategory',
  editCategory ='/ost-service/table/updateCategory',
  deleteCategory = '/ost-service/table/deleteCategory',
  // deleteBatch = '/ost-service/table/batchDeleteTable',

}

/**
 * 列表接口
 * @param params
 */
export const listTable = (params) => {
  const headers = {}
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.get({url: Api.list, params, headers});
}

export const listTableCategory = (params) => {
  const headers = {}
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.get({url: Api.listCategory, params, headers});
}


/**
 * 删除
 */
export const deleteTable = (params, handleSuccess) => {
  const headers = {}
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.delete({ url: Api.deleteTable, params, headers}, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

export const deleteCategory = (params, handleSuccess) => {
  const headers = {}
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.delete({ url: Api.deleteCategory, params, headers}, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}


/**
 * 批量删除
 * @param params
 */
export const batchDeleteTable = (params, handleSuccess) => {
  const headers = {}
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params, headers}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}


/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdateTable = (params, isUpdate) => {
  const headers = {}
  console.log('params.tenantId', params.tenantId);
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.post({url: isUpdate ? Api.edit : Api.save, params, headers});
}

export const saveOrUpdateCategory = (params, isUpdate) => {
  const headers = {}
  console.log('params.tenantId', params.tenantId);
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.post({url: isUpdate ? Api.editCategory : Api.saveCategory, params, headers});
}
